package me.kaede.tagview;

import ohos.agp.components.AttrHelper;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.Optional;

/**
 * ResolutionUtil
 *
 * @since 2021-06-04
 */
public class ResolutionUtil {

    /**
     * dpToPx
     *
     * @param context
     * @param dipValue
     * @return int
     */
    public static int dpToPx(Context context, float dipValue) {
        DisplayAttributes displayAttributes = getScreenPiex(context);
        return (int) (dipValue * displayAttributes.scalDensity);
    }

    /**
     * spToPx
     *
     * @param context
     * @param spValue
     * @return int
     */
    public static int spToPx(Context context, float spValue) {
        DisplayAttributes displayAttributes = getScreenPiex(context);
        return (int) (spValue * displayAttributes.densityPixels);
    }

    /**
     * getScreenWidth
     *
     * @param context
     * @return int
     */
    public static int getScreenWidth(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        Point point = new Point();
        display.getSize(point);
        return (int) point.getPointX();
    }

    /**
     * 获取屏幕密度
     *
     * @param context
     * @return DisplayAttributes
     */
    public static DisplayAttributes getScreenPiex(Context context) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        DisplayAttributes displayAttributes = null;
        if (display.isPresent()) {
            displayAttributes = display.get().getAttributes();
        }
        return displayAttributes;
    }
}
